package com.eric.husky.service;

import com.eric.husky.common.utils.UpdateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.eric.husky.dao.PositionDao;
import com.eric.husky.bean.Position;
import java.util.List;

@Service
public class PositionService {
    @Autowired
	private PositionDao positionDao;

	public Page<Position> list(Position position, Pageable page) {
		Example<Position> example = Example.of(position);
		return positionDao.findAll(example, page);
	}

	public Position get(Integer locationid) {
		return positionDao.findById(locationid).get();
	}

    public List<Position> findList() {
		return positionDao.findAll();
	}

	@Transactional
	public Position save(Position position) {
	    if(position.getLocationid()!=null){
	        Position positionOld = positionDao.findById(position.getLocationid()).orElse(null);
	        if(positionOld!=null){
	            UpdateUtil.copyNullProperties(position,positionOld);
                return positionDao.save(positionOld);
	        }
	    }
	    return positionDao.save(position);
	}

	@Transactional
	public void delete(Integer locationid) {
		positionDao.deleteById(locationid);
	}

}